﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Care
{
    public partial class CareList : System.Web.UI.Page
    {
        public string userid = "";
        public string strseo = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            userid = Request.QueryString["id"] + "";
            if (userid == "")
            {
                Response.Redirect("../Index.aspx");
                return;
            }
            //userid = userid.Substring(0, userid.Length - 1);
            if (!Page.IsPostBack)
            {
                BindCareUser();
                BindRecommand();//该用户推荐
                BindSeo();
            }
        }

        #region 加载该用户关注的用户列表
        private void BindCareUser()
        {
            //当前用户数据
            Users uinfo = new Users();
            UsersHelper uhelper = new UsersHelper();
            uinfo = uhelper.GetUsers(userid);
            string currentusernickname = uinfo.NickName;
            litNickName1.Text = currentusernickname;
            litNickName2.Text = currentusernickname;
            litBrief.Text = uinfo.Brief;
            litBlogNum.Text = uinfo.BlogNum.ToString();
            litToBeFriendNum.Text = uinfo.ToBeFriendNum.ToString();
            litFriendNum.Text = uinfo.FriendNum.ToString();
            hidBlogUserID.Value = userid;
            //当前用户的权限
            string roleid = uinfo.RoleID;
            Role roleinfo = new Role();
            RoleHelper rolehelper = new RoleHelper();
            roleinfo = rolehelper.GetRole(roleid);
            litRoleName.Text = roleinfo.RoleName;

            //该用户关注的用户列表
            FriendHelper fhelper = new FriendHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", userid);
            List<Friend> flist = fhelper.QueryFriendBySearch(c, new Order[] { new Order("CreateDate", OrderMode.Desc) });

            dlCareList.DataSource = flist;
            dlCareList.DataBind();
        }
        #endregion

        #region 加载该用户推荐的
        private void BindRecommand()
        {
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);

            Criteria c1 = new Criteria();
            RecommandHelper rhelper = new RecommandHelper();
            Criteria crecommand = new Criteria();
            crecommand.Add(CriteriaType.Equals, "UsersID", userid);
            crecommand.Add(CriteriaType.Equals, "ObjectType", 1);
            List<Recommand> rlist = rhelper.QueryRecommandBySearch(crecommand, new Order[] { new Order("CreateDate") });
            if (rlist != null && rlist.Count > 0)
            {
                c1.Mode = CriteriaMode.Or;
                for (int j = 0; j < rlist.Count; j++)
                {
                    string objectid = "";
                    Recommand rinfo = rlist[j];
                    objectid = rinfo.ObjectID;
                    Criteria cTemp = new Criteria(CriteriaType.Equals, "ID", objectid);
                    c1.Criterias.Add(cTemp);
                }
                c.Criterias.Add(c1);
            }

            BlogHelper bhelper = new BlogHelper();
            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
            DataTable dtpvBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("RecommandCount", OrderMode.Desc) }, 0, 10, fields);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtpvBlog.Columns.Add(dcTitle);
            dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
            dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

            if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtpvBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 26);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }
            }

            rptRecommandOrder.DataSource = dtpvBlog;
            rptRecommandOrder.DataBind();
        }
        #endregion

        #region 加载SEO信息
        private void BindSeo()
        {

            string nickname = litNickName1.Text;
            string systemkeyword = System.Configuration.ConfigurationManager.AppSettings["keyword"] + "";

            string author = System.Configuration.ConfigurationManager.AppSettings["author"] + "";
            string copyright = System.Configuration.ConfigurationManager.AppSettings["copyright"] + "";

            string keyword = nickname + " " + systemkeyword + " " + author;
            string description = litBrief.Text;
            string title = nickname + "关注的用户-" + author;

            strseo += "<title>" + title + "</title>\r\n";
            strseo += "<meta name=\"keywords\" content=\"" + keyword + "\" />\r\n";
            strseo += "<meta name=\"description\" content=\"" + description + "\" />\r\n";
            strseo += "<meta name=\"author\" content=\"" + author + "\" />\r\n";
            strseo += "<meta name=\"Copyright\" content=\"" + copyright + "\" />\r\n";
        }
        #endregion
    }
}